﻿#region Header

/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/

#endregion Header

namespace Probel.Ldoc.ViewModels.Dto
{
    using System.Collections.Generic;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Mvvm.Gui;

    public class RecordDrawerViewModel : TreeViewModel
    {
        #region Constructors

        public RecordDrawerViewModel(RecordDrawerDto drawer, IErrorService errorService, IPatientService service, bool isAsync)
            : base(errorService, service, isAsync)
        {
            this.Id = drawer.Id;
            this.Name = drawer.Title;
            this.Records = this.ToViewModel(drawer.Cards);
        }

        #endregion Constructors

        #region Properties

        public IEnumerable<RecordCardViewModel> Records
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        protected override void Rename()
        {
            if (this.Name == this.CachedName) { return; }

            this.Service.UpdateRecordDrawerName(new RecordDrawerCardDto() { Id = Id, Title = this.Name });
        }

        private IEnumerable<RecordCardViewModel> ToViewModel(IEnumerable<RecordCardDto> cards)
        {
            var result = new List<RecordCardViewModel>();
            foreach (var item in cards)
            {
                result.Add(new RecordCardViewModel(item, this.ErrorService, this.Service, this.IsAsync));
            }
            return result;
        }

        #endregion Methods
    }
}